
# Create your models here.
from django.db import models
from django.contrib.auth.models import AbstractUser
import uuid

# 自定义用户模型（替换默认用户）
class User(AbstractUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    pid = models.CharField("身份证号", max_length=18, unique=True)  # 身份证号
    phone = models.CharField("手机号", max_length=11, unique=True)   # 手机号
    address = models.TextField("地址", blank=True, null=True)       # 地址

    def __str__(self):
        return self.username

# 银行卡模型
class BankCard(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    card_number = models.CharField("卡号", max_length=19, unique=True)  # 银行卡号
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="cards", verbose_name="用户")
    balance = models.DecimalField("余额", max_digits=15, decimal_places=2, default=0.00)  # 余额
    created_at = models.DateTimeField("创建时间", auto_now_add=True)  # 开卡时间
    is_lost = models.BooleanField("是否挂失", default=False)          # 挂失状态

    def __str__(self):
        return self.card_number

# 交易记录模型
class Transaction(models.Model):
    # 交易类型：存款、取款、转账
    TRANSACTION_TYPES = (
        ('DEPOSIT', '存款'),
        ('WITHDRAW', '取款'),
        ('TRANSFER', '转账'),
    )
    # 交易状态：成功、失败、处理中
    TRANSACTION_STATUS = (
        ('SUCCESS', '成功'),
        ('FAILED', '失败'),
        ('PENDING', '处理中'),
    )

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    card = models.ForeignKey(BankCard, on_delete=models.CASCADE, related_name="transactions", verbose_name="银行卡")
    transaction_type = models.CharField("交易类型", max_length=10, choices=TRANSACTION_TYPES)
    amount = models.DecimalField("金额", max_digits=15, decimal_places=2)  # 交易金额
    target_card = models.CharField("目标卡号", max_length=19, blank=True, null=True)  # 转账目标卡号
    operator = models.CharField("操作员", max_length=50)  # 操作员（系统/用户/管理员）
    status = models.CharField("状态", max_length=10, choices=TRANSACTION_STATUS, default='PENDING')
    created_at = models.DateTimeField("交易时间", auto_now_add=True)  # 交易时间
    remark = models.TextField("备注", blank=True, null=True)  # 备注信息

    def __str__(self):
        return f"{self.get_transaction_type_display()} - {self.amount}"